﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Net.NetworkInformation;
using System.IO.IsolatedStorage;
using System.IO;

namespace Senior_Project
{
    public partial class MoreHistoryPage : PhoneApplicationPage
    {
        App app = App.Current as App;
        UserProfile user;
        string uid;

        public MoreHistoryPage()
        {
            // ##################################### ONLINE MODE ############################################
            InitializeComponent();

            // open userXML.xml file and read data
            string test;
            try
            {
                // set variable refer to IsolatedStorageFile
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // set variable refer to IsolatedStorageFileStream
                    IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("userXML.xml", FileMode.Open);
                    using (StreamReader reader = new StreamReader(isoFileStream))
                    {
                        // read to end file
                        test = reader.ReadToEnd();

                        // set element for feed data from userXML.xml file
                        XElement profileElement = XElement.Parse(test);
                        var profileItems = from profileItem in profileElement.Descendants("user")
                                           select user = new UserProfile
                                           {
                                               Uid = profileItem.Element("uid").Value
                                           };
                        int countp = profileItems.Count();
                        if (countp == 1)
                        {
                            uid = user.Uid;
                        }
                    }
                }
            }
            catch { MessageBox.Show("File not found !"); }

            // check internet connection status
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                MessageBox.Show("No internet connection, please try agian later");
            }

            // set client
            Random rand = new Random();
            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri("http://" + app.url + "/tracking/mobile/cross1day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            // sum value of each value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0;
            double sumCarbo = 0, sumCalcuim = 0, sumPhospho = 0, sumIron = 0;

            // set element value
            XElement element = XElement.Parse(e.Result);
            var items = from item in element.Descendants("user")
                        where item.Element("uid").Value == uid
                        select new Data
                        {
                            Weight = item.Element("weight").Value,
                            Calory = item.Element("calorie").Value,
                            Protein = item.Element("protein").Value,
                            Fat = item.Element("fat").Value,
                            Carbohydrate = item.Element("cabohydrate").Value,
                            Calcium = item.Element("calcuim").Value,
                            PhosPhorus = item.Element("phosphorus").Value,
                            Iron = item.Element("iron").Value
                        };
            int count = items.Count();


            //sum value of each variable
            foreach (var sumItems in items)
            {
                sumWeight += Convert.ToDouble(sumItems.Weight);
                sumCalorie += Convert.ToDouble(sumItems.Calory);
                sumProtein += Convert.ToDouble(sumItems.Protein);
                sumFat += Convert.ToDouble(sumItems.Fat);
                sumCarbo += Convert.ToDouble(sumItems.Carbohydrate);
                sumCalcuim += Convert.ToDouble(sumItems.Calcium);
                sumPhospho += Convert.ToDouble(sumItems.PhosPhorus);
                sumIron += Convert.ToDouble(sumItems.Iron);
            }


            //show result to TextBox >> ReadOnly mode
            weight.Text = sumWeight.ToString() + " g.";
            calory.Text = sumCalorie.ToString() + " kcal.";
            protein.Text = sumProtein.ToString() + " g.";
            fat.Text = sumFat.ToString() + " g.";
            carbo.Text = sumCarbo.ToString() + " g.";
            calcuim.Text = sumCalcuim.ToString() + " g.";
            phospho.Text = sumPhospho.ToString() + " g.";
            iron.Text = sumIron.ToString() + " g.";
        }



        // **************************************** menu bar "day" checked !! ****************************************************
        private void day_Click(object sender, System.EventArgs e)
        {
            Random rand = new Random();
            WebClient dayClient = new WebClient();
            dayClient.DownloadStringAsync(new Uri("http://" + app.url + "/tracking/mobile/cross1day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
            dayClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(dayClient_DownloadStringCompleted);
        }

        void dayClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            // sum value of each value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0;
            double sumCarbo = 0, sumCalcuim = 0, sumPhospho = 0, sumIron = 0;

            // set element value
            XElement element = XElement.Parse(e.Result);
            var items = from item in element.Descendants("user")
                        where item.Element("uid").Value == uid
                        select new Data
                        {
                            Weight = item.Element("weight").Value,
                            Calory = item.Element("calorie").Value,
                            Protein = item.Element("protein").Value,
                            Fat = item.Element("fat").Value,
                            Carbohydrate = item.Element("cabohydrate").Value,
                            Calcium = item.Element("calcuim").Value,
                            PhosPhorus = item.Element("phosphorus").Value,
                            Iron = item.Element("iron").Value
                        };
            int count = items.Count();


            //sum value of each variable
            foreach (var sumItems in items)
            {
                sumWeight += Convert.ToDouble(sumItems.Weight);
                sumCalorie += Convert.ToDouble(sumItems.Calory);
                sumProtein += Convert.ToDouble(sumItems.Protein);
                sumFat += Convert.ToDouble(sumItems.Fat);
                sumCarbo += Convert.ToDouble(sumItems.Carbohydrate);
                sumCalcuim += Convert.ToDouble(sumItems.Calcium);
                sumPhospho += Convert.ToDouble(sumItems.PhosPhorus);
                sumIron += Convert.ToDouble(sumItems.Iron);
            }


            //show result to TextBox >> ReadOnly mode
            weight.Text = sumWeight.ToString() + " g.";
            calory.Text = sumCalorie.ToString() + " kcal.";
            protein.Text = sumProtein.ToString() + " g.";
            fat.Text = sumFat.ToString() + " g.";
            carbo.Text = sumCarbo.ToString() + " g.";
            calcuim.Text = sumCalcuim.ToString() + " g.";
            phospho.Text = sumPhospho.ToString() + " g.";
            iron.Text = sumIron.ToString() + " g.";
        }
        // ************************************************************************************************************************



        // **************************************** menu bar "week" checked !! ****************************************************
        private void saven_Click(object sender, System.EventArgs e)
        {
            Random rand = new Random();
            WebClient weekClient = new WebClient();
            weekClient.DownloadStringAsync(new Uri("http://" + app.url + "/tracking/mobile/cross7day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
            weekClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(weekClient_DownloadStringCompleted);
        }

        void weekClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            // sum value of each value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0;
            double sumCarbo = 0, sumCalcuim = 0, sumPhospho = 0, sumIron = 0;

            // set element value
            XElement element = XElement.Parse(e.Result);
            var items = from item in element.Descendants("user")
                        where item.Element("uid").Value == uid
                        select new Data
                        {
                            Weight = item.Element("weight").Value,
                            Calory = item.Element("calorie").Value,
                            Protein = item.Element("protein").Value,
                            Fat = item.Element("fat").Value,
                            Carbohydrate = item.Element("cabohydrate").Value,
                            Calcium = item.Element("calcuim").Value,
                            PhosPhorus = item.Element("phosphorus").Value,
                            Iron = item.Element("iron").Value
                        };


            //sum value of each variable
            foreach (var sumItems in items)
            {
                sumWeight += Convert.ToDouble(sumItems.Weight);
                sumCalorie += Convert.ToDouble(sumItems.Calory);
                sumProtein += Convert.ToDouble(sumItems.Protein);
                sumFat += Convert.ToDouble(sumItems.Fat);
                sumCarbo += Convert.ToDouble(sumItems.Carbohydrate);
                sumCalcuim += Convert.ToDouble(sumItems.Calcium);
                sumPhospho += Convert.ToDouble(sumItems.PhosPhorus);
                sumIron += Convert.ToDouble(sumItems.Iron);
            }


            //show result to TextBox >> ReadOnly mode
            weight.Text = sumWeight.ToString() + " g.";
            calory.Text = sumCalorie.ToString() + " kcal.";
            protein.Text = sumProtein.ToString() + " g.";
            fat.Text = sumFat.ToString() + " g.";
            carbo.Text = sumCarbo.ToString() + " g.";
            calcuim.Text = sumCalcuim.ToString() + " g.";
            phospho.Text = sumPhospho.ToString() + " g.";
            iron.Text = sumIron.ToString() + " g.";
        }
        // ************************************************************************************************************************




        // **************************************** menu bar "month" checked !! ****************************************************
        private void thirty_Click(object sender, System.EventArgs e)
        {
            // set RandDom variable and Client
            Random rand = new Random();
            WebClient monthClient = new WebClient();
            monthClient.DownloadStringAsync(new Uri("http://" + app.url + "/tracking/mobile/cross30day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
            monthClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(monthClient_DownloadStringCompleted);
        }

        void monthClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            // sum value of each value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0;
            double sumCarbo = 0, sumCalcuim = 0, sumPhospho = 0, sumIron = 0;

            // set element value
            XElement element = XElement.Parse(e.Result);
            var items = from item in element.Descendants("user")
                        where item.Element("uid").Value == uid
                        select new Data
                        {
                            Weight = item.Element("weight").Value,
                            Calory = item.Element("calorie").Value,
                            Protein = item.Element("protein").Value,
                            Fat = item.Element("fat").Value,
                            Carbohydrate = item.Element("cabohydrate").Value,
                            Calcium = item.Element("calcuim").Value,
                            PhosPhorus = item.Element("phosphorus").Value,
                            Iron = item.Element("iron").Value
                        };


            //sum value of each variable
            foreach (var sumItems in items)
            {
                sumWeight += Convert.ToDouble(sumItems.Weight);
                sumCalorie += Convert.ToDouble(sumItems.Calory);
                sumProtein += Convert.ToDouble(sumItems.Protein);
                sumFat += Convert.ToDouble(sumItems.Fat);
                sumCarbo += Convert.ToDouble(sumItems.Carbohydrate);
                sumCalcuim += Convert.ToDouble(sumItems.Calcium);
                sumPhospho += Convert.ToDouble(sumItems.PhosPhorus);
                sumIron += Convert.ToDouble(sumItems.Iron);
            }


            //show result to TextBox >> ReadOnly mode
            weight.Text = sumWeight.ToString() + " g.";
            calory.Text = sumCalorie.ToString() + " kcal.";
            protein.Text = sumProtein.ToString() + " g.";
            fat.Text = sumFat.ToString() + " g.";
            carbo.Text = sumCarbo.ToString() + " g.";
            calcuim.Text = sumCalcuim.ToString() + " g.";
            phospho.Text = sumPhospho.ToString() + " g.";
            iron.Text = sumIron.ToString() + " g.";
        }
        // ***************************************************************************************************************
    }
}